package cn.huiyunche.base.service.interfaces.impl;

import cn.huiyunche.base.service.interfaces.BScoresLevelService;
import cn.huiyunche.base.service.mappers.BScoresLevelMapper;
import cn.huiyunche.base.service.model.BScoresLevel;
import cn.huiyunche.base.service.model.BScoresLevelExample;
import cn.huiyunche.base.service.model.BScoresLevelExample.Criteria;
import cn.huiyunche.tools.basic.exceptions.BusinessException;
import com.alibaba.fastjson.JSON;
import io.jsonwebtoken.lang.Collections;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * 积分等级 接口实现类
 *
 * @author lm
 */
@Service
public class BScoresLevelServiceImpl implements BScoresLevelService {

    private static final Logger LOGGER = LoggerFactory.getLogger(BScoresLevelServiceImpl.class);

    @Autowired
    private BScoresLevelMapper bScoresLevelMapper = null;

    private BScoresLevelMapper getBScoresLevelMapper() {
        return this.bScoresLevelMapper;
    }

    /**
     * 根据用户类型 用户等级 查询等级
     *
     * @param userType
     * @param level
     * @return
     */
    @Override
    public BScoresLevel selectByLevel(Integer userType, int level) {
        LOGGER.info("select level by userType : {}, level : {}.", userType, level);
        try {
            BScoresLevelExample example = new BScoresLevelExample();
            example.createCriteria().andUserTypeEqualTo(userType).andLevelEqualTo(level);
            List<BScoresLevel> bScoresLevels = this.getBScoresLevelMapper().selectByExample(example);
            if (!Collections.isEmpty(bScoresLevels)) {
                return bScoresLevels.get(0);
            }
        } catch (Exception e) {
            LOGGER.info("select level by userType : {}, level : {}, error.", userType, level, e);
            throw new BusinessException("查询用户等级异常");
        }
        return null;
    }

    /**
     * 根据用户类型和里程数 查询用户级别
     *
     * @param userType
     * @param total
     */
    @Override
    public BScoresLevel selectByLevel(Integer userType, BigDecimal total) {
        LOGGER.info("select level params userType : {}, total : {}.", userType, total);
        try {
            BScoresLevelExample example = new BScoresLevelExample();
            BScoresLevelExample example2 = new BScoresLevelExample();

            Criteria createCriteria = example2.createCriteria();
            createCriteria.andUserTypeEqualTo(userType).andBeginRangeLessThanOrEqualTo(total).andEndRangeIsNull();

            example.createCriteria().andUserTypeEqualTo(userType).andBeginRangeLessThanOrEqualTo(total).
                    andEndRangeGreaterThanOrEqualTo(total);
            example.or(createCriteria);

            List<BScoresLevel> bScoresLevels = this.getBScoresLevelMapper().selectByExample(example);
            LOGGER.info("select level params userType : {}, total : {}, bScoresLevels : {}.", userType, total, JSON.toJSONString(bScoresLevels));
            if (!Collections.isEmpty(bScoresLevels)) {
                LOGGER.info("select level params userType : {}, total : {}, bScoresLevel : {}.", userType, total, JSON.toJSONString(bScoresLevels.get(0)));
                return bScoresLevels.get(0);
            }
        } catch (Exception e) {
            LOGGER.error("select level params userType : {}, total : {}, error : {}.", userType, total, e);
            throw new BusinessException("根据用户里程数查询用户级别异常");
        }
        return null;
    }

}
